package cn.iocoder.yudao.module.service.controller.admin.leavemessage;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.annotation.security.PermitAll;
import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;

import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.service.controller.admin.leavemessage.vo.*;
import cn.iocoder.yudao.module.service.dal.dataobject.leavemessage.ServiceLeaveMessageDO;
import cn.iocoder.yudao.module.service.service.leavemessage.ServiceLeaveMessageService;

@Tag(name = "管理后台 - 客户留言")
@RestController
@RequestMapping("/service/leave-message")
@Validated
public class ServiceLeaveMessageController {

    @Resource
    private ServiceLeaveMessageService leaveMessageService;

    @PostMapping("/create")
    @Operation(summary = "创建客户留言")
    @PermitAll
    @OperateLog(enable = false)
    public CommonResult<Long> createLeaveMessage(@Valid @RequestBody ServiceLeaveMessageSaveReqVO createReqVO) {
        return success(leaveMessageService.createLeaveMessage(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新客户留言")
    @PreAuthorize("@ss.hasPermission('service:leave-message:update')")
    public CommonResult<Boolean> updateLeaveMessage(@Valid @RequestBody ServiceLeaveMessageUpdateReqVO updateReqVO) {
        leaveMessageService.updateLeaveMessage(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除客户留言")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('service:leave-message:delete')")
    public CommonResult<Boolean> deleteLeaveMessage(@RequestParam("id") Long id) {
        leaveMessageService.deleteLeaveMessage(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得客户留言")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('service:leave-message:query')")
    public CommonResult<ServiceLeaveMessageRespVO> getLeaveMessage(@RequestParam("id") Long id) {
        ServiceLeaveMessageDO leaveMessage = leaveMessageService.getLeaveMessage(id);
        return success(BeanUtils.toBean(leaveMessage, ServiceLeaveMessageRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得客户留言分页")
    @PreAuthorize("@ss.hasPermission('service:leave-message:query')")
    public CommonResult<PageResult<ServiceLeaveMessageRespVO>> getLeaveMessagePage(@Valid ServiceLeaveMessagePageReqVO pageReqVO) {
        PageResult<ServiceLeaveMessageDO> pageResult = leaveMessageService.getLeaveMessagePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ServiceLeaveMessageRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出客户留言 Excel")
    @PreAuthorize("@ss.hasPermission('service:leave-message:export')")
    @OperateLog(type = EXPORT)
    public void exportLeaveMessageExcel(@Valid ServiceLeaveMessagePageReqVO pageReqVO,
                                        HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ServiceLeaveMessageDO> list = leaveMessageService.getLeaveMessagePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "客户留言.xls", "数据", ServiceLeaveMessageRespVO.class,
                BeanUtils.toBean(list, ServiceLeaveMessageRespVO.class));
    }

}